REPORT zmm_material_report.

*----------------------------------------------------------------------*
*  TYPE DECLARATIONS
*----------------------------------------------------------------------*
TYPES:
  BEGIN OF ty_material,
    matnr TYPE matnr,        " 物料编码
    mtart TYPE mtart,        " 物料类型
    maktx TYPE maktx,        " 物料描述
    werks TYPE werks_d,      " 工厂
    matkl TYPE matkl,        " 物料组
    meins TYPE meins,        " 基本单位
  END OF ty_material.

DATA: gt_material TYPE STANDARD TABLE OF ty_material,
      gs_material TYPE ty_material.

*----------------------------------------------------------------------*
*  SELECTION SCREEN
*----------------------------------------------------------------------*
SELECT-OPTIONS: s_werks FOR gs_material-werks OBLIGATORY.

*----------------------------------------------------------------------*
*  START-OF-SELECTION
*----------------------------------------------------------------------*
START-OF-SELECTION.
  PERFORM get_material_data.
  PERFORM display_alv.

*----------------------------------------------------------------------*
*  FORM get_material_data
*----------------------------------------------------------------------*
FORM get_material_data.
  SELECT
    mara~matnr
    mara~mtart
    makt~maktx
    marc~werks
    mara~matkl
    mara~meins
  INTO CORRESPONDING FIELDS OF TABLE gt_material
  FROM mara
  INNER JOIN marc ON mara~matnr = marc~matnr
  INNER JOIN makt ON mara~matnr = makt~matnr
  WHERE marc~werks IN s_werks
    AND makt~spras = sy-langu.
ENDFORM.                    "get_material_data

*----------------------------------------------------------------------*
*  表单：显示ALV报表
*----------------------------------------------------------------------*
FORM display_alv.
  " 调用ALV网格显示函数
  DATA: gs_layout TYPE lvc_s_layo.
  gs_layout-zebra = 'X'.        " 斑马条纹显示
  gs_layout-colwidth_opt = 'X'. " 自动优化列宽

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = sy-repid
      i_callback_user_command = 'USER_COMMAND'  " 注册用户命令回调
      it_fieldcat        = gt_fieldcat
      i_grid_title       = '物料信息报表'
      i_layout           = gs_layout
    TABLES
      t_outtab           = gt_material
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.
  " 检查函数调用是否成功
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
ENDFORM.                    "显示ALV报表

*----------------------------------------------------------------------*
*  FIELD CATALOG BUILDING
*----------------------------------------------------------------------*
DATA: gt_fieldcat TYPE lvc_t_fcat,
      gs_fieldcat TYPE lvc_s_fcat.

INITIALIZATION.
  PERFORM build_fieldcat.

FORM build_fieldcat.
  " 清空字段目录内表
  CLEAR gt_fieldcat.

  " 物料编码字段
  gs_fieldcat-fieldname = 'MATNR'.
  gs_fieldcat-ref_table = 'MARA'.
  gs_fieldcat-ref_field = 'MATNR'.
  gs_fieldcat-coltext   = '物料编码'.
  gs_fieldcat-outputlen = 18.
  gs_fieldcat-hotspot   = 'X'.  " 设置为热点字段
  APPEND gs_fieldcat TO gt_fieldcat.
  CLEAR gs_fieldcat.

  " 物料类型字段
  gs_fieldcat-fieldname = 'MTART'.
  gs_fieldcat-ref_table = 'MARA'.
  gs_fieldcat-ref_field = 'MTART'.
  gs_fieldcat-coltext   = '物料类型'.
  gs_fieldcat-outputlen = 4.
  APPEND gs_fieldcat TO gt_fieldcat.
  CLEAR gs_fieldcat.

  " 物料描述字段
  gs_fieldcat-fieldname = 'MAKTX'.
  gs_fieldcat-ref_table = 'MAKT'.
  gs_fieldcat-ref_field = 'MAKTX'.
  gs_fieldcat-coltext   = '物料描述'.
  gs_fieldcat-outputlen = 40.
  APPEND gs_fieldcat TO gt_fieldcat.
  CLEAR gs_fieldcat.

  " 工厂字段
  gs_fieldcat-fieldname = 'WERKS'.
  gs_fieldcat-ref_table = 'MARC'.
  gs_fieldcat-ref_field = 'WERKS'.
  gs_fieldcat-coltext   = '工厂'.
  gs_fieldcat-outputlen = 4.
  APPEND gs_fieldcat TO gt_fieldcat.
  CLEAR gs_fieldcat.

  " 物料组字段
  gs_fieldcat-fieldname = 'MATKL'.
  gs_fieldcat-ref_table = 'MARA'.
  gs_fieldcat-ref_field = 'MATKL'.
  gs_fieldcat-coltext   = '物料组'.
  gs_fieldcat-outputlen = 9.
  APPEND gs_fieldcat TO gt_fieldcat.
  CLEAR gs_fieldcat.

  " 基本单位字段
  gs_fieldcat-fieldname = 'MEINS'.
  gs_fieldcat-ref_table = 'MARA'.
  gs_fieldcat-ref_field = 'MEINS'.
  gs_fieldcat-coltext   = '基本单位'.
  gs_fieldcat-outputlen = 3.
  APPEND gs_fieldcat TO gt_fieldcat.
  CLEAR gs_fieldcat.
ENDFORM.                    "构建字段目录

*----------------------------------------------------------------------*
*  表单：处理用户命令（双击事件）
*----------------------------------------------------------------------*
FORM user_command USING r_ucomm LIKE sy-ucomm
                       rs_selfield TYPE slis_selfield.
  " 检查是否是双击热点字段事件
  IF r_ucomm = '&IC1' AND rs_selfield-fieldname = 'MATNR'.
    " 获取选中的物料编码
    READ TABLE gt_material INDEX rs_selfield-tabindex INTO gs_material.
    IF sy-subrc = 0.
      " 调用事务代码MM03显示物料主数据
      SET PARAMETER ID 'MAT' FIELD gs_material-matnr.
      CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN.
    ENDIF.
  ENDIF.
ENDFORM.                    "user_command